﻿'*************************************************************************
'@Tác giả : Trần Quốc Việt
'@Tên file: FrmCustomReport.vb
'@Ngày tạo: 06/08/2010
'@Mục đích: Form hiển thị báo cáo Customize
'*************************************************************************
Imports System.Drawing
Imports System.Windows.Forms
Imports DevExpress.XtraReports.UserDesigner
Imports DevExpress.XtraReports.UI
Imports DevExpress.XtraPrinting.Drawing
Imports Publics
Imports DevExpress.XtraBars

Public Class FrmCustomReport
    Private DuocSuaBaoCao As Boolean = False

    Public Shared BaoCaoChotCaMau1 As Integer = 1
    Public Shared BaoCaoNhapXuat1Cap As Integer = 2
    Public Shared CongNoTongHopSieuThi As Integer = 3


    'Phat hanh  => Sieu thi khong cho sua mau lam gi ca
    'Public Shared SuaMau_BaoCaoBanHangKhongGhiHoaDon As Boolean
    'Public Shared SuaMau_BaoCaoBanHangTheoNhanVienThuNgan As Boolean
    'Public Shared SuaMau_BaoCaoBanHangTheoThoiGianTrongNgay As Boolean
    'Public Shared SuaMau_InHoaDon As Boolean
    'Public Shared SuaMau_InMaVach As Boolean

    Private strPath As String
    Private rpt As XtraReport

    ''' <summary>
    ''' Hàm khởi tạo có truyền tham số
    ''' </summary>
    ''' <param name="obj">Đối tượng báo cáo XtraReport</param>
    ''' <param name="strPath">Đường dẫn + tên file đến thư mục có file *.repx</param>
    ''' <param name="iType">Chọn tên loại báo cáo</param>
    ''' <remarks></remarks>
    Public Sub New(ByVal obj As XtraReport, ByVal strPath As String, ByVal iType As Integer)
        InitializeComponent()
        Select Case iType
            Case CongNoTongHopSieuThi
                rpt = CType(obj, rptCongNoTongHopSieuThi)
            Case BaoCaoNhapXuat1Cap
                rpt = CType(obj, rptBaoCaoNhapXuat1Cap)
            Case BaoCaoChotCaMau1
                rpt = CType(obj, rptBaoCaoChotCaMau1)

        End Select
        Me.strPath = strPath
        DuocSuaBaoCao = False
        btnSuaMau.Visibility = IIf(DuocSuaBaoCao, BarItemVisibility.Always, BarItemVisibility.Never)
    End Sub

    Private Sub FrmCustomReport_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        printControl.PrintingSystem = rpt.PrintingSystem
        If Not vDaDangKy Then
            rpt.Watermark.Text = "CHƯA ĐĂNG KÝ BẢN QUYỀN"
        Else
            rpt.Watermark.Text = ""
        End If
        rpt.Watermark.TextDirection = DirectionMode.ForwardDiagonal
        rpt.Watermark.Font = New Font(rpt.Watermark.Font.FontFamily, 30, FontStyle.Bold)
        rpt.Watermark.ForeColor = Color.DodgerBlue
        rpt.Watermark.TextTransparency = 150
        rpt.Watermark.ShowBehind = True
        rpt.CreateDocument()
    End Sub

    Private Sub btnSuaMau_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnSuaMau.ItemClick
        Dim frm As New FrmCustomDesign
        Dim obj As New SaveCommandHandler(frm.designPanel, strPath)
        frm.WindowState = FormWindowState.Maximized
        frm.designPanel.AddCommandHandler(obj)
        frm.designPanel.OpenReport(rpt)
        frm.ShowDialog()
        If obj.isRefresh Then
            frm.designPanel.CloseReport()
            rpt.CreateDocument()
            SaveLog("Sửa mẫu báo cáo", "Sửa mẫu báo cáo", "Tên file: " & rpt.Name.Substring(0, rpt.Name.Length - 1) & " Đường dẫn: " & strPath)
        Else
            frm.designPanel.CloseReport()
        End If
    End Sub

    ''' <summary>
    ''' Key down  
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub FrmCustomReport_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        If e.KeyCode = Keys.Escape Then
            Me.Close()
        End If
    End Sub
End Class

Public Class SaveCommandHandler
    Implements ICommandHandler
    Public isRefresh As Boolean = False
    Private strPath As String
    Private panel As XRDesignPanel

    Public Sub New(ByVal panel As XRDesignPanel, ByVal strPath As String)
        Me.panel = panel
        Me.strPath = strPath
    End Sub

    Public Overridable Sub HandleCommand(ByVal command As ReportCommand, _
    ByVal args() As Object, ByRef handled As Boolean) Implements ICommandHandler.HandleCommand
        If Not CanHandleCommand(command) Then
            Return
        End If
        Save()
        handled = True
        isRefresh = True
    End Sub

    Public Overridable Function CanHandleCommand(ByVal command As  _
    ReportCommand) As Boolean Implements ICommandHandler.CanHandleCommand
        Return command = ReportCommand.SaveFile Or command = ReportCommand.SaveFileAs 'Or command = ReportCommand.Closing
    End Function

    Sub Save()
        panel.Report.SaveLayout(strPath)
        panel.ReportState = ReportState.Saved
    End Sub
End Class

Public Enum TypeBaoCao
    XemTonKho
    BCNhapXuatTonKho
    BCHangGuiBan
    TheKho
    TheoDoiTienDoPH
    TheoDoiTienDoNhapKho
    CanDoiKeHoach
    XemHDNhap
    XemHDXuat
    BCNhapKho
    BCXuatKho
    BCVanChuyenNhap
    BCVanChuyenXuat
    BCTTBXNhap
    BCTTBXXuat
End Enum
